/* =============================================================
 * date-utils.js v1.1.0
 * =============================================================
 * Original written by Ysy
 * =============================================================
 * Date 15/06/2021
 * ============================================================ */


define(["jquery", "CommonUtils", "NumberUtils", "StringUtils"], function ($, CommonUtils, NumberUtils, StringUtils) {
    let DateUtils = {
        /**
         * @Description 日期格式转字符串格式
         * @param date  需要格式化的日期
         * @param fmt   格式化类型  yyyy-MM-dd hh:mm:ss.S
         * @returns {*|boolean|void|Promise<unknown>|Promise<any>|string}
         */
        dateToString: function (date, fmt) {
            let o = {
                "M+": date.getMonth() + 1,                 // 月份
                "d+": date.getDate(),                      // 日
                "h+": date.getHours(),                     // 小时
                "m+": date.getMinutes(),                   // 分
                "s+": date.getSeconds(),                   // 秒
                // "q+": Math.floor((data.getMonth() + 3) / 3), // 季度
                "q+": Math.floor(NumberUtils.division(NumberUtils.addition(date.getMonth(), 3), 3)), // 季度
                "S": date.getMilliseconds()                // 毫秒
            };
            if (/(y+)/.test(fmt))
                fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (let k in o)
                if (new RegExp("(" + k + ")").test(fmt))
                    fmt = fmt.replace(RegExp.$1, (Object.is(RegExp.$1.length, 1)) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        },

        /**
         * @Description 获取当前日期
         * @Date 10:30 2021/6/15
         * @param fmt   格式化类型  yyyy-MM-dd hh:mm:ss.S
         * @returns {*|boolean|void|Promise<unknown>|Promise<any>|string}
         */
        getNowDate: function (fmt) {
            let date = new Date();
            return this.dateToString(date, fmt);
        },

        /**
         * @Description // 获取当前日期
         * @Date 10:30 2021/6/15
         **/
        getNowWeek: function () {
            let days = new Date().getDay();

            switch (days) {
                case 1:
                    days = "星期一";
                    break;
                case 2:
                    days = "星期二";
                    break;
                case 3:
                    days = "星期三";
                    break;
                case 4:
                    days = "星期四";
                    break;
                case 5:
                    days = "星期五";
                    break;
                case 6:
                    days = "星期六";
                    break;
                case 0:
                    days = "星期日";
                    break;
            }

            return days;
        },

        /**
         * @Descripttion: 时间戳转日期
         * @Author: Ysy
         * @Date: 2021-07-07 15:06:04
         * @param {*} date
         * @param {*} fmt
         * @return {*}
         */
        timeStampToDate: function (timeStamp, fmt) { // DateUtils.timeStampToDate(1622952000000,"yyyy-MM-dd hh:mm:ss");

            if (CommonUtils.isString(timeStamp)) {
                timeStamp = new Date(parseInt(timeStamp));
            } else if (CommonUtils.isNumber(timeStamp)) {
                timeStamp = new Date(timeStamp);
            }


            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (timeStamp.getFullYear() + '').substr(4 - RegExp.$1.length));
            }

            let o = {
                'M+': timeStamp.getMonth() + 1,
                'd+': timeStamp.getDate(),
                'h+': timeStamp.getHours(),
                'm+': timeStamp.getMinutes(),
                's+': timeStamp.getSeconds()
            };

            let padLeftZero = (str) => {
                return ('00' + str).substr(str.length);
            }

            for (let k in o) {
                if (new RegExp(`(${k})`).test(fmt)) {
                    let str = o[k] + '';
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
                }
            }

            return fmt;
        },
    };

    return DateUtils;
});


